/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farent.solutions.common.discovery;

import com.farent.solutions.common.discovery.ClassResolver.ClassSelectionStrategy;

/**
 * A Test that checks to see if each class is assignable to the provided class. Note
 * that this test will match the parent type itself if it is presented for matching.
 * @author Farouk.Alhassan
 */
public class IsAssignableFromSelectionStrategy implements ClassSelectionStrategy {

    private Class<?> parent;

    public IsAssignableFromSelectionStrategy() {
    }

    
    /** Constructs an IsA test using the supplied Class as the parent class/interface. */
    public IsAssignableFromSelectionStrategy(Class<?> parentType) {
        this.parent = parentType;
    }

    /** Returns true if type is assignable to the parent type supplied in the constructor. */
    @SuppressWarnings("unchecked")
    @Override
    public boolean matches(Class type) {
        return type != null && parent.isAssignableFrom(type);
    }

    @Override
    public String toString() {
        return "is assignable to " + parent.getSimpleName();
    }

    @Override
    public void setClass(Class<?> type) {
        this.parent = type;
    }
}
